<?php
/**
 *
 * 版权所有：安德玛
 * 作    者：安德玛
 * 版本控制：
			20210128---接入微信支付
 * 功能说明：订单支付控制器。
 *
 **/
namespace Api\Controller;
class PayOrderController extends ComController
{
    /**
     * 根据订单号请求支付接口（会员等级）
     */
    public function userLevel()
    {
        if(!IS_POST){
            $this->res['code'] = 101;
            $this->res['msg'] = '请发送post请求';
            $this->response($this->res,'json');
        }
        $post_data = file_get_contents('php://input');
        $json = json_decode($post_data);
        $order_no = $json->order_no;
        
        if(!$order_no){
            $this->res['code'] = 102;
            $this->res['msg'] = '订单号为空';
            $this->response($this->res,'json');
        }
        $order_info = M('user_level_buy')->where(array('order_no'=>$order_no))->find();
        if(empty($order_info)){
            $this->res['code'] = 103;
            $this->res['msg'] = '无效订单';
            $this->response($this->res,'json');
        }
        // 封装支付参数
        // $price = $order_info['money'];
        // $pay_type = M('pay_type')->where('id='.$order_info['pay_id'])->field('o')->find();
        // $istype = $pay_type['o'];
        // $user_level = M('user_level')->where('id='.$order_info['level_id'])->field('title')->find();
        // $goodsname = $user_level['title'];
        // $orderuid = $this->user['username'];
        // $orderid = $order_no;
        // $uid = "9d76dafe0a549ec6f76a6c04";
        // $token = "da4d1ab652ff92567cff29e86eaff931";
        // $return_url = C('SITE_URL').'/payinfo?orderno='.$order_no;
        // $notify_url = C('URL').'/api/paynotify/userLevel';
        // $key = md5($goodsname. $istype . $notify_url . $orderid . $orderuid . $price . $return_url . $token . $uid);
        // $returndata['goodsname'] = $goodsname;
        // $returndata['istype'] = $istype;
        // $returndata['key'] = $key;
        // $returndata['notify_url'] = $notify_url;
        // $returndata['orderid'] = $orderid;
        // $returndata['orderuid'] =$orderuid;
        // $returndata['price'] = $price;
        // $returndata['return_url'] = $return_url;
        // $returndata['uid'] = $uid;
		
		
		/*
		封装请求参数
		*/
	   //商户ID
	   $alipay_config['partner']		= '843324';
	   
	   //商户KEY
	   $alipay_config['key']			= 'A39CEA077A2E1F5E82212D0604CA9F56';
	   $alipay_config['sign_type']    = strtoupper('MD5');
	   
	   //字符编码格式 目前支持 gbk 或 utf-8
	   $alipay_config['input_charset']= strtolower('utf-8');
	   
	   //访问模式,根据自己的服务器是否支持ssl访问，若支持请选择https；若不支持请选择http
	   $alipay_config['transport']    = 'http';
	   
	   //支付API地址
	   $alipay_config['apiurl']    = 'https://vip.wmiis.cn/';
	   //异步通知返回
	   $notify_url = C('URL').'/api/paynotify/userLevel';
	   //页面跳转
	   $return_url = C('SITE_URL').'/user';
	   // $return_url = 'https://www.baidu.com/';
	   //商户订单号
	   $out_trade_no = $order_no;
	   //支付方式
	   $type = 'wxpay';
	   //商品名称
	   $user_level = M('user_level')->where('id='.$order_info['level_id'])->field('title')->find();
	   $goodsname = $user_level['title'];
	   $name = $goodsname;
	   //付款金额
	   $price = $order_info['money'];
	   $money = $price;
	   //站点名称
	   $sitename = '安德玛官方网站';
	   //构造要请求的参数数组，无需改动
	   $parameter = array(
	   		"pid" => trim($alipay_config['partner']),
	   		"type" => $type,
	   		"notify_url"	=> $notify_url,
	   		"return_url"	=> $return_url,
	   		"out_trade_no"	=> $out_trade_no,
	   		"name"	=> $name,
	   		"money"	=> $money,
	   		"sitename"	=> $sitename
	   );
	  //建立请求
	  vendor('SDK.lib.epay_submit#class');
	  $alipaySubmit = new \AlipaySubmit($alipay_config);
	  $html_text = $alipaySubmit->buildRequestForm($parameter);
	  
	   
	   
	   
	   
		
		
        $this->res['data'] = $html_text;
        $this->response($this->res,'json');
    }
    
    
    /**
     * 根据订单号请求支付接口（任务发布）
     */
    public function publishTask()
    {
        if(!IS_POST){
            $this->res['code'] = 101;
            $this->res['msg'] = '请发送post请求';
            $this->response($this->res,'json');
        }
        $post_data = file_get_contents('php://input');
        $json = json_decode($post_data);
        $order_id = $json->order_id;
        if(!$order_id){
            $this->res['code'] = 102;
            $this->res['msg'] = '订单id为空';
            $this->response($this->res,'json');
        }
        $order_info = M('task')->where(array('id'=>$order_id))->find();
        if(empty($order_info)){
            $this->res['code'] = 103;
            $this->res['msg'] = '无效订单';
            $this->response($this->res,'json');
        }
        // 封装支付参数
        $price = $order_info['amount_price'];
        $pay_type = M('pay_type')->where('id='.$order_info['pay_id'])->field('o')->find();
        $istype = $pay_type['o'];
        $goodsname = $order_info['title'];
        $orderuid = $this->user['username'];
        $orderid = $order_id;
        $uid = "9d76dafe0a549ec6f76a6c04";
        $token = "da4d1ab652ff92567cff29e86eaff931";
        $return_url = C('SITE_URL').'/addtaskdetail/'.$order_id;
        $notify_url = C('URL').'/api/paynotify/publishTask';
        $key = md5($goodsname. $istype . $notify_url . $orderid . $orderuid . $price . $return_url . $token . $uid);
        $returndata['goodsname'] = $goodsname;
        $returndata['istype'] = $istype;
        $returndata['key'] = $key;
        $returndata['notify_url'] = $notify_url;
        $returndata['orderid'] = $orderid;
        $returndata['orderuid'] =$orderuid;
        $returndata['price'] = $price;
        $returndata['return_url'] = $return_url;
        $returndata['uid'] = $uid;
        //$result = send_post('https://pay.bearsoftware.net.cn/', $returndata);
        $this->res['data'] = $returndata;
        $this->response($this->res,'json');
    }
}